/*-
 * Copyright 2009-2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __list_h__
#define __list_h__
//-----------------------------------------------------------------------------
#include "config.h"
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
struct list_node {
  struct list_node * prev;
  struct list_node * next;
};
//---------------------------------------------------------------------------
struct list {
  struct list_node * head;
  struct list_node * tail;
  uintptr_t count;
  uintptr_t node;
};
//-----------------------------------------------------------------------------
struct list * list_new(void);
void list_init(struct list * lst);
void list_set_node(struct list * lst,const void * data,const struct list_node * node);
void list_delete(struct list * lst);
void list_destroy(struct list * lst);
//-----------------------------------------------------------------------------
void list_insert_head(struct list * lst,void * data);
void list_insert_tail(struct list * lst,void * data);
void list_remove(struct list * lst,void * data);
//---------------------------------------------------------------------------
void list_insert_before(struct list * lst,void * before_data,void * data);
void list_insert_after(struct list * lst,void * after_data,void * data);
//-----------------------------------------------------------------------------
static __inline__
struct list_node * list_data2node(struct list * lst,void * data)
{
  return (struct list_node *) ((uint8_t *) data + lst->node);
}
//---------------------------------------------------------------------------
static __inline__
void * list_node2data(struct list * lst,struct list_node * node)
{
  return (uint8_t *) node - lst->node;
}
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
